package com.lineage.server.serverpackets;

import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1ItemStatus;

public class S_ItemStatus extends ServerBasePacket {
  private byte[] _byte;
  
  public S_ItemStatus(L1ItemInstance item) {
    this._byte = null;
    if (item == null)
      return; 
    buildPacket(item);
  }
  
  private void buildPacket(L1ItemInstance item) {
    writeC(24);
    writeD(item.getId());
    writeS(item.getViewName());
    int count = (int)Math.min(item.getCount(), 2000000000L);
    writeD(count);
    if (!item.isIdentified()) {
      writeC(0);
    } else {
      L1ItemStatus itemInfo = new L1ItemStatus(item);
      byte[] status = itemInfo.getStatusBytes(false).getBytes();
      writeC(status.length);
      byte[] array;
      int length = (array = status).length;
      int i = 0;
      while (i < length) {
        byte b = array[i];
        writeC(b);
        i++;
      } 
    } 
  }
  
  public S_ItemStatus(L1ItemInstance item, long count) {
    this._byte = null;
    writeC(24);
    writeD(item.getId());
    writeS(item.getNumberedViewName(count));
    int out_count = (int)Math.min(count, 2000000000L);
    writeD(out_count);
    if (!item.isIdentified()) {
      writeC(0);
    } else {
      L1ItemStatus itemInfo = new L1ItemStatus(item);
      byte[] status = itemInfo.getStatusBytes(false).getBytes();
      writeC(status.length);
      byte[] array;
      int length = (array = status).length;
      int i = 0;
      while (i < length) {
        byte b = array[i];
        writeC(b);
        i++;
      } 
    } 
  }
  
  public S_ItemStatus(L1ItemInstance item, boolean isIdentified) {
    this._byte = null;
    writeC(24);
    writeD(item.getId());
    writeS(item.getViewName());
    int count = (int)Math.min(item.getCount(), 2000000000L);
    writeD(count);
    if (!isIdentified) {
      writeC(0);
    } else {
      L1ItemStatus itemInfo = new L1ItemStatus(item);
      byte[] status = itemInfo.getStatusBytes(true).getBytes();
      writeC(status.length);
      byte[] array;
      int length = (array = status).length;
      int i = 0;
      while (i < length) {
        byte b = array[i];
        writeC(b);
        i++;
      } 
    } 
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
